﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BuildingTomorow;

namespace Testing_components
{
    /// <summary>
    /// Interaction logic for LoginPanel.xaml
    /// </summary>
    public partial class LoginPanel : UserControl
    {
        private RulesTab _tab;
        public LoginPanel(RulesTab tab)
        {
            InitializeComponent();
            _tab = tab;
        }

        private void buttonLogin_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxUsername.Text == "" || textBoxPassword.Password == "")
            {
                labelMessage.Content = "Please fill all fields";
            }
            else
            {
                labelMessage.Content = "";
                if (DataAccess.GetInstance().HasUser(textBoxUsername.Text.Trim()))
                {
                    if (DataAccess.GetInstance().isValidAccount(textBoxUsername.Text, textBoxPassword.Password))
                    {
                        _tab.removeLogin();
                    }
                    else
                    {
                        labelMessage.Content = "Wrong password";
                    }
                }
                else
                {
                    labelMessage.Content = "Wrong Username";
                }
            }
            
        }

        private void buttonLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                buttonLogin_Click(this, new RoutedEventArgs());
            }
        }
    }

}
